define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'order/index/index' + location.search,
                    add_url: 'order/index/add',
                    edit_url: 'order/index/edit',
                    del_url: 'order/index/del',
                    multi_url: 'order/index/multi',
                    table: 'order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'sort',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'manystore.nickname', title: '商家'},
                        {field: 'total_price', title: __('Total_price'), operate:'BETWEEN'},
                        {field: 'pack', title: __('Pack'), operate:'BETWEEN'},
                        {field: 'payment', title: __('Payment'), searchList: {"0":__('Payment 0'),"1":__('Payment 1'),"2":__('Payment 2')}, formatter: Table.api.formatter.normal},
                        {field: 'type', title: __('Type'), searchList: {"0":__('Type 0'),"1":__('Type 1')}, formatter: Table.api.formatter.normal},
                        {field: 'shipping_fee', title: __('Shipping_fee')},
                        {field: 'orderid', title: __('Orderid')},
                        {field: 'order_time', title: __('Order_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'finish_time', title: __('Finish_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3'),"4":__('Status 4'),"5":__('Status 5'),"6":__('Status 6'),"7":__('Status 7'),"8":__('Status 8')}, formatter: Table.api.formatter.status},
                        {field: 'site_id', title: __('Site_id')},
                        {field: 'courier.truename', title: __('Courier_id')},
                        {field: 'user.nickname', title: '用户名'},
                        {field: 'operate', title: __('Operate'), table: table,
                          buttons: [
                                    {name: 'detail',title: '详情', icon: 'fa fa-list', classname: 'btn btn-xs btn-primary btn-dialog', url: '#'}
                        ],
                         events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                         formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                $(table).data("operate-del", null);
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});